package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.GoodsTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsTypeDao goodsTypeDao;
    @Autowired
    private GoodsTypeService goodsTypeService;

    @Override
    public PageInfo<Goods> getIndexGoodsByPageAndCondition(String codeOrName, Integer goodsTypeId) {
        // 获取全部分类
        List<GoodsType> allCategoryList = goodsTypeDao.getGoodsCategory();
        // 制作断言Map，用来判断这个分类有没有子分类
        Map<Integer, String> predicatedMap = goodsTypeService.createPredicatedMap(allCategoryList);
        List<Integer> goodsTypeIds = getLeaveIds(goodsTypeId, allCategoryList, predicatedMap);
        List<Goods> indexGoodsList = goodsDao.getIndexGoodsByPageAndCondition(codeOrName, goodsTypeIds);
        return new PageInfo<>(indexGoodsList);
    }

    @Override
    public ServiceVO getCode() {
        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();
        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;
        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();
        for (int i = 4; i > intCode.toString().length(); i--) {
            unitCode = "0" + unitCode;
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public List<Unit> getAllUnits() {
        return goodsDao.getAllUnits();
    }

    @Override
    public PageInfo<Goods> getGoodsByPageAndCondition(String goodsName, Integer goodsTypeId) {
        // 获取全部分类
        List<GoodsType> allCategoryList = goodsTypeDao.getGoodsCategory();
        // 制作断言Map，用来判断这个分类有没有子分类
        Map<Integer, String> predicatedMap = goodsTypeService.createPredicatedMap(allCategoryList);
        List<Integer> goodsTypeIds = getLeaveIds(goodsTypeId, allCategoryList, predicatedMap);
        List<Goods> goodsList = goodsDao.queryAllGoodsByCondition(goodsName, goodsTypeIds);
        return new PageInfo<>(goodsList);
    }

    @Override
    public void saveGoods(Goods goods) {
        goodsDao.saveGoods(goods);
    }

    @Override
    public void updateGoodsById(Goods goods) {
        goodsDao.updateGoodsById(goods);
    }

    @Override
    public boolean deleteGoodsById(Integer goodsId) {
        Goods goods = goodsDao.findGoodsById(goodsId);
        if (goods.getState() != 0) {
            return false;
        }
        goodsDao.deleteGoodsById(goodsId);
        return true;
    }

    @Override
    public PageInfo<Goods> getNoStockGoodsByPageAndCondition(String nameOrCode) {
        List<Goods> noStockGoodsList = goodsDao.getNoStockGoodsByCondition(nameOrCode);
        return new PageInfo<>(noStockGoodsList);
    }

    @Override
    public PageInfo<Goods> getHasStockGoodsByPageAndCondition(String nameOrCode) {
        List<Goods> hasStockGoodsList = goodsDao.getHasStockGoodsByPageAndCondition(nameOrCode);
        return new PageInfo<>(hasStockGoodsList);
    }

    @Override
    public void setInventoryQuantityAndPurchasingPrice(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.setInventoryQuantityAndPurchasingPrice(goodsId, inventoryQuantity, purchasingPrice);
    }

    @Override
    public boolean deleteStockById(Integer goodsId) {
        Goods goods = goodsDao.findGoodsById(goodsId);
        if (goods.getState() != 0) {
            return false;
        }
        goodsDao.deleteStockById(goodsId);
        return true;
    }

    private List<Integer> getLeaveIds(Integer goodsTypeId, List<GoodsType> allCategoryList, Map<Integer, String> predicatedMap) {
        List<Integer> goodsTypeIds = new ArrayList<>();
        List<GoodsType> subGoodsTypeList = allCategoryList.stream()
                .filter(goodsType -> goodsType.getPId().equals(goodsTypeId))
                .collect(Collectors.toList());
        for (GoodsType goodsType : subGoodsTypeList) {
            Integer subGoodsTypeId = goodsType.getGoodsTypeId();
            if (Objects.nonNull(predicatedMap.get(subGoodsTypeId))) { // 非叶子
                goodsTypeIds.addAll(getLeaveIds(subGoodsTypeId, allCategoryList, predicatedMap));
            } else { // 叶子
                goodsTypeIds.add(subGoodsTypeId);
            }
        }
        return goodsTypeIds;
    }
}